package com.cims.common.utility;

import java.io.UnsupportedEncodingException;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import com.opensymphony.xwork2.ActionContext;

public class StringUtils {
	private static final char SPACE_CHAR_HALF = ' ';

	private static final char SPACE_CHAR_WID = '　';

	private StringUtils() {
		// Static Function only
		// No instance may be created
	}

	public static String fillNumberMaxLen(String strValue, int iMax) {
		String strReturn = "";
		int iLen = 0;

		if (strValue == null) {
			iLen = 0;
		} else {
			strReturn = strValue.trim();
			iLen = strReturn.length();
		}

		for (int i = 0; i < (iMax - iLen); i++) {
			strReturn = "0" + strReturn;
		}

		return strReturn;
	}

	public static String fillSpace(String str, int idxNumber) {
		if (str == null || str.equals("")) {
			return str;
		}

		try {
			if (str.getBytes("MS932").length < idxNumber) {
				for (int i = 0; i < idxNumber - str.getBytes("MS932").length; i++) {
					str += " ";
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return str;
	}

	public static String toString(Object value, String defValue) {
		return ((value != null) ? value.toString() : defValue);
	}

	public static String toString(Object value) {
		return toString(value, null);
	}

	public static String toString(Object[] array, String defValue) {
		String s = arrayToString(array, "/");
		return (s != null ? s : defValue);
	}

	public static String toString(Object[] array) {
		return toString(array, null);
	}

	public static boolean containsMatch(String val, String pattern) {
		String rep = val.replaceFirst(pattern, "###match###");

		if (!rep.equals(val)) {
			return true;
		}
		return false;
	}

	public static String emptyIfNull(String strIn) {

		String strTmp = "";

		if (strIn != null) {
			strTmp = strIn.trim();
		}

		return strTmp;
	}

	public static String valueOf(Object value) {
		return toString(value, "");
	}

	public static String valueOf(Object[] array) {
		return toString(array, "");
	}

	public static String coalesce(String preferred, String alternative) {
		return isValid(preferred) ? preferred : alternative;
	}

	public static String nullIfEmpty(Object value) {
		if (value == null)
			return null;
		String strval = value.toString();
		return ((strval.length() == 0) ? null : strval);
	}

	public static String arrayToString(Object[] array, String separator) {
		if (array == null || array.length < 1)
			return null; // Empty
		if (array.length > 1) { // multi Column Key
			StringBuilder buf = new StringBuilder();
			for (int i = 0; i < array.length; i++) {
				if (i > 0)
					buf.append(separator);
				buf.append(array[i]);
			}
			return buf.toString();
		}
		// Only one member
		return array[0].toString();
	}

	public static String[] stringToArray(String string, String separator) {
		if (string == null || isEmpty(separator))
			return null; // Empty
		// Count the items first
		int sepLength = separator.length();
		int count = 0;
		int pos = -1;
		while ((pos = string.indexOf(separator, pos + sepLength)) >= 0)
			count++;
		// Alloc an array
		String[] array = new String[count + 1];
		if (count > 0) {
			int beg = 0;
			for (int i = 0; true; i++) {
				int end = string.indexOf(separator, beg);
				if (end > beg) { // Add Item
					array[i] = string.substring(beg, end);
					beg = end + sepLength;
				} else { // Last Item
					array[i] = string.substring(beg);
					break;
				}
			}
		} else {
			array[0] = string;
		}
		// Only one member
		return array;
	}

	public static String collectionToString(Collection<? extends Object> c, String separator) {
		if (c == null || c.size() == 0)
			return null; // Empty
		// Iterator
		StringBuilder buf = new StringBuilder();
		boolean addSep = false;
		Iterator<?> i = c.iterator();
		while (i.hasNext()) {
			if (addSep)
				buf.append(separator);
			buf.append(valueOf(i.next()));
			addSep = true;
		}
		return buf.toString();
	}

	public static boolean isEmpty(String s) {
		return (s == null || s.trim().length() == 0);
	}

	public static boolean isValid(String s) {
		return (s != null && s.trim().length() > 0);
	}

	public static boolean isEmail(String s) {
		int indexOfAtChar = s.indexOf("@");
		if (indexOfAtChar > 0) {
			int indexOfDotChar = s.indexOf(".", indexOfAtChar);
			if (indexOfDotChar > 0) {
				return true;
			}
			return false;
		}
		return false;
	}

	public static String validate(String s) {
		if (s == null)
			return null;
		s = s.trim();
		if (s.length() == 0)
			return null;
		return s;
	}

	public static String replace(String source, String find, String replace) {
		// Check params
		if (source == null || find == null || find.length() == 0)
			return source;
		// Find the character
		int index = source.indexOf(find);
		if (index < 0)
			return source;
		if (replace == null)
			replace = "";
		// replace and find again
		int len = find.length();
		return source.substring(0, index) + replace + replace(source.substring(index + len), find, replace);
	}

	public static String replaceAll(String source, String find, String replace) {
		if (source == null)
			return null;
		if (find == null) {
			find = "";
		}
		if (replace == null) {
			replace = "";
		}
		source = trim(source);
		int fromLength = find.length();
		int start = source.indexOf(find);
		if (start == -1)
			return source;

		boolean greaterLength = (replace.length() >= fromLength);

		StringBuilder buffer;
		// If the "to" parameter is longer than (or
		// as long as) "from", the final length will
		// be at least as large
		if (greaterLength) {
			if (find.equals(replace))
				return source;
			buffer = new StringBuilder(source.length());
		} else {
			buffer = new StringBuilder();
		}

		char[] origChars = source.toCharArray();

		int copyFrom = 0;
		while (start != -1) {
			buffer.append(origChars, copyFrom, start - copyFrom);
			buffer.append(replace);
			copyFrom = start + fromLength;
			start = source.indexOf(find, copyFrom);
		}
		buffer.append(origChars, copyFrom, origChars.length - copyFrom);

		return buffer.toString();
	}

	public static String replaceBRbyLF(String s) {
		return replaceAll(replaceAll(s, "<br/>", "\n\n"), "<br />", "\n\n");
	}

	public static String replaceLFByBR(String s) {
		if (s == null) {
			return "";
		} else
			return s.replaceAll("\n\n", "<br/>").replaceAll("\r\n", "<br/>");
	}

	public static String trimAll(String orig) {
		if (orig == null)
			return "";
		String str = orig.trim();
		StringBuilder strBuf = new StringBuilder(str.length());
		boolean hasSpace = false;
		for (int i = 0, j = 0; i < str.length(); i++) {
			if (str.charAt(i) == ' ') {
				if (!hasSpace) {
					strBuf.append(' ');
					hasSpace = true;
				}
			} else {
				j = str.indexOf(' ', i);
				if (j == -1)
					j = str.length();
				strBuf.append(str.substring(i, j));
				hasSpace = false;
				i = j - 1;
			}
		}

		return strBuf.toString();
	}

	public static String trimRight(String orig) {
		if (orig == null)
			return null;
		String str = orig.trim();

		return str;
	}

	public static byte[] getBytes(String strData) {

		byte ret[];

		try {
			// ret = strData.getBytes("Windows-31J");

			// MSSQL2008: Japanese_CI_AS(encoding MS932)
			ret = strData.getBytes("MS932");
		} catch (UnsupportedEncodingException e) {
			ret = new byte[0];
		}
		return ret;
	}

	public static Character getCharacter(String strData) {

		if (strData != null && trim(strData).length() > 0) {
			return strData.charAt(0);
		}
		return null;
	}

	public static boolean isInteger(final String number) {
		boolean isInteger;

		try {
			Integer.parseInt(number);
			isInteger = true;
		} catch (NumberFormatException nfe) {
			// eat the exception
			isInteger = false;
		}

		return isInteger;
	}

	public static boolean isCode(final String code) {
		char c;
		String codeTrim = code.trim();
		try {
			for (int index = 0; index < codeTrim.length(); index++) {
				c = codeTrim.charAt(index);
				// if ((c>='A' && c<='Z') || (c>='0' && c<='9') || (c>='a' && c<='z') || (c>='?? && c<='??)) {
				if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
					continue;
				} else
					return false;
			}
			return true;
		} catch (Exception nfe) {
			// eat the exception
			return false;
		}
	}

	public static boolean isAscii(final String code) {
		char c;
		String codeTrim = code.trim();
		try {
			for (int index = 0; index < codeTrim.length(); index++) {
				c = codeTrim.charAt(index);
				if (c >= 32 && c <= 126) {// 32: Space, 126: ~
					continue;
				} else
					return false;
			}
			return true;
		} catch (Exception nfe) {
			// eat the exception
			return false;
		}
	}

	public static boolean isTel(final String tel) {
		char c;
		try {
			for (int index = 0; index < tel.length(); index++) {
				c = tel.charAt(index);
				// if ((c>='0' && c<='9') || (c>='?? && c<='??) || c=='-' || c=='ãƒ¼') {
				if ((c >= '0' && c <= '9') || c == '-') {
					continue;
				} else
					return false;
			}
			return true;
		} catch (Exception nfe) {
			// eat the exception
			return false;
		}
	}

	public static String filterNumber(final String code) {
		char c;
		String result = "";

		for (int index = 0; index < code.length(); index++) {
			c = code.charAt(index);
			if (c >= '0' && c <= '9') {
				result = result + c;
			}
		}
		return result;
	}

	public static String filterNonAscii(String inString) {
		// Create the encoder and decoder for the character encoding
		Charset charset = Charset.forName("US-ASCII");
		CharsetDecoder decoder = charset.newDecoder();
		CharsetEncoder encoder = charset.newEncoder();
		// This line is the key to removing "unmappable" characters.
		encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
		String result = inString;

		try {
			// Convert a string to bytes in a ByteBuffer
			java.nio.ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(inString));

			// Convert bytes in a ByteBuffer to a character ByteBuffer and then to a string.
			CharBuffer cbuf = decoder.decode(bbuf);
			result = cbuf.toString();
		} catch (CharacterCodingException cce) {
			System.out.println("Exception during character encoding/decoding: " + cce.getMessage());
		}

		return result;
	}
	public static boolean isDouble(final String number) {
		boolean isDoubler;

		try {
			Double.parseDouble(number);
			isDoubler = true;
		} catch (NumberFormatException nfe) {
			// eat the exception
			isDoubler = false;
		}

		return isDoubler;
	}
	public static String convertString(String strValue) {
		return (strValue != null ? strValue.trim() : "");
	}
	public static String trimString(String strValue) {
		return (strValue != null ? strValue : "").trim();
	}
	public static int getLengthUTF8(String strValue) {
		int length = 0;

		if (!isValid(strValue))
			length = 0;

		try {
			length = strValue.getBytes("UTF-8").length;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			length = 0;
		}
		return length;
	}
	public static int getLengthMS932(String strValue) {
		int length = 0;

		if (!isValid(strValue))
			length = 0;

		try {
			length = strValue.getBytes("MS932").length;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			length = 0;
		}
		return length;
	}
	
	public static int getDB2LenString(String strValue) {

		int iLen = 0;
		int iCount = 0;
		int iPreCount = 0;
		int iCurrentCount = 0;

		if (strValue == null || "".equalsIgnoreCase(strValue)) {
			iLen = 0;
		} else {

			for (int i = 0; i < strValue.length(); i++) {

				try {
					iCount = strValue.substring(i, i + 1).getBytes("Shift_JIS").length;

					iCurrentCount = iCount;
				} catch (UnsupportedEncodingException e) {
					iCount = 2;
				}

				if (i == 0) {
					iPreCount = iCount;

					if (iCount >= 2) {
						iLen = 1;
					}
				}

				if (iCurrentCount != iPreCount)
					iLen += 1;

				iLen += iCount;

				iPreCount = iCount;
			}
		}

		if (iCurrentCount >= 2)
			iLen++;

		return iLen;
	}
	public static boolean checkMSSQLLenString(String strValue, int iMaxLen) {

		int iLen = getLengthMS932(strValue);

		if (iLen > iMaxLen)
			return false;

		return true;
	}

	public static boolean checkDB2LenString(String strValue, int iMaxLen) {

		int iLen = getDB2LenString(strValue);

		if (iLen > iMaxLen)
			return false;

		return true;
	}
	
	public static String tooltip(String strValue, int width) {
		strValue = convertString(strValue);
		String tooltip = strValue;
		if (strValue.length() > width) {
			String displayString = strValue.substring(0, width);
			tooltip = displayString + "<span onmouseout=\"hideTooltip()\"";
			tooltip += " onmouseover=\"showTooltip(event,'" + strValue + "');return false\">" + "...</span>";
		}
		return tooltip;
	}

	public static String tooltip(String strValue, String strTooltip) {
		strValue = convertString(strValue);
		strTooltip = convertString(strTooltip);
		String tooltip = strValue;
		if (strTooltip.length() > 0) {
			tooltip = "<span onmouseout=\"hideTooltip()\"";
			tooltip += " onmouseover=\"showTooltip(event,'" + strTooltip + "');return false\">" + strValue + "</span>";
		}
		return tooltip;
	}

	public static int getNumberDate(String strDate) {

		int iNumberValue = 0;
		String strTemp = "";

		if (StringUtils.isValid(strDate) == false) {
			iNumberValue = 0;
		} else {
			try {
				strTemp = strDate.replace("/", "").replace(":", "");
				iNumberValue = Integer.parseInt(strTemp);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				iNumberValue = 0;
			}
		}

		return iNumberValue;
	}

	public static String getFormatHour(int iHour) {

		String strHour = "";

		if ((iHour > 9) || (iHour < -9)) {
			strHour = "" + iHour;
		} else if ((iHour >= 0) && (iHour <= 9)) {
			strHour = "0" + iHour;
		} else if ((iHour < 0) && (iHour >= -9)) {
			strHour = "-0" + (-1) * iHour;
		}
		return strHour;
	}

	public static String getFormatMinute(int iMinute) {

		String strHour = "";

		if (iMinute > 9) {
			strHour = "" + iMinute;
		} else {
			strHour = "0" + iMinute;
		}
		return strHour;
	}

	public static String rightLen(String value, Character c, int len) {
		value = convertString(value).trim();
		String temp = value;
		if (value.length() < len) {
			for (int i = 0; i < len - value.length(); i++) {
				temp = c + temp;
			}

		}
		return temp;
	}

	public static String padRight(String value, Character c, int len) {
		value = convertString(value).trim();
		String temp = value;
		if (value.length() < len) {
			for (int i = 0; i < len - value.length(); i++) {
				temp = temp + c;
			}

		}
		return temp;
	}

	public static String padLeft(String value, Character c, int len) {
		value = convertString(value).trim();
		String temp = value;
		if (value.length() < len) {
			for (int i = 0; i < len - value.length(); i++) {
				temp = c + temp;
			}

		}
		return temp;
	}

	public static int getHourHHMMSS(String time) {
		try {
			if ((StringUtils.convertString(time)).trim().length() > 0) {
				time = rightLen(time, '0', 6);
				return Integer.parseInt(time.substring(0, 2));
			} else {
				return 0;
			}
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
			return 0;
		}

	}

	public static String getFormatTimeSum(Integer iTimeSum) {
		if (iTimeSum != null && iTimeSum > 0) {
			return StringUtils.getFormatHour(iTimeSum / 10000) + ":" + StringUtils.getFormatMinute((iTimeSum % 10000) / 100);
		} else {
			return "";
		}
	}

	public static String getFormatTime(Integer iTime) {
		if (iTime != null && iTime > 0) {
			return StringUtils.getFormatHour(iTime / 10000) + ":" + StringUtils.getFormatMinute((iTime % 10000) / 100);
		} else {
			return "";
		}
	}

	public static String getFormatDoubleTime(Double iTime) {
		if (iTime != null && iTime > 0) {
			return StringUtils.getFormatDoubleHour(NumberUtils.getRoundFloor(0, iTime / 10000)) + ":"
					+ StringUtils.getFormatDoubleMinute(NumberUtils.getRoundFloor(0, (iTime % 10000) / 100));
		} else {
			return "";
		}
	}

	public static String getFormatDoubleTimeMinute(Double iTime) {
		if (iTime != null && iTime > 0) {
			return StringUtils.getFormatHourWithoutZezo(NumberUtils.getRoundFloor(0, iTime / 60)) + "."
					+ StringUtils.getFormatDoubleMinute(iTime - 60 * NumberUtils.getRoundFloor(0, iTime / 60));
		} else {
			return "0.0";
		}
	}

	public static String getFormatHourWithoutZezo(double iHour) {

		String strHour = "";

		strHour = "" + iHour;

		if (strHour.indexOf(".") > -1) {
			strHour = strHour.substring(0, strHour.indexOf("."));
		}

		return strHour;
	}

	public static String getFormatDoubleHour(double iHour) {

		String strHour = "";

		if ((iHour > 9) || (iHour < -9)) {
			strHour = "" + iHour;
		} else if ((iHour >= 0) && (iHour <= 9)) {
			strHour = "0" + iHour;
		} else if ((iHour < 0) && (iHour >= -9)) {
			strHour = "-0" + (-1) * iHour;
		}

		if (strHour.indexOf(".") > -1) {
			strHour = strHour.substring(0, strHour.indexOf("."));
		}

		return strHour;
	}

	public static String getFormatDoubleMinute(double iMinute) {

		String strMinute = "";

		if (iMinute > 9) {
			strMinute = "" + iMinute;
		} else {
			strMinute = "0" + iMinute;
		}

		if (strMinute.indexOf(".") > -1) {
			strMinute = strMinute.substring(0, strMinute.indexOf("."));
		}

		return strMinute;
	}

	public static int getMinuteHHMMSS(String time) {
		try {
			if ((StringUtils.convertString(time)).trim().length() > 0) {
				time = rightLen(time, '0', 6);
				return Integer.parseInt(time.substring(2, 4));
			} else {
				return 0;
			}
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
			return 0;
		}

	}

	public static String getFormatZipCode(String strZipCode) {
		if (strZipCode == null)
			return "";

		if (strZipCode.length() > 3) {
			String str1 = strZipCode.substring(0, 3);
			String str2 = strZipCode.substring(3);
			strZipCode = str1 + "-" + str2;
		}

		return strZipCode;
	}

	public static String getFormatDate(int iDate) {

		String strDate = "" + iDate;
		String strReturn = "";

		if (strDate.length() < 8) {
			strReturn = "";
		} else {
			strReturn += strDate.substring(0, 4);
			strReturn += "/" + strDate.substring(4, 6);
			strReturn += "/" + strDate.substring(6, 8);
		}

		return strReturn;
	}

	public static String getFormatDate(String strDate) {

		String strReturn = "";

		if (strDate.length() < 8) {
			strReturn = "";
		} else {
			strReturn += strDate.substring(0, 4);
			strReturn += "/" + strDate.substring(4, 6);
			strReturn += "/" + strDate.substring(6, 8);
		}

		return strReturn;
	}

	public static String getFormatTime(String strTime) {

		String strReturn = "";

		if (strTime == null || strTime.length() < 5 || strTime.length() > 7) {
			strReturn = "";
		} else if (strTime.length() == 5) {
			strReturn += strTime.substring(0, 1);
			strReturn += ":" + strTime.substring(1, 3);
			strReturn += ":" + strTime.substring(3, 5);
		} else if (strTime.length() == 6) {
			strReturn += strTime.substring(0, 2);
			strReturn += ":" + strTime.substring(2, 4);
			strReturn += ":" + strTime.substring(4, 6);
		}

		return strReturn;
	}
	
	public static String getFileName(String strFilePath) {
		String strName = strFilePath.substring(strFilePath.lastIndexOf("\\") + 1);
		return strName;
	}

	public static String convertIntegerToStr(Integer iValue) {

		if (iValue == null)
			return "";
		else
			return iValue.toString();

	}

	public static String convertDoubleToStr(Double iValue) {

		if (iValue == null)
			return "";
		else
			return iValue.toString();

	}
	

	public static Long converString2Long(String strValue){
		Long iValue = null;
		try {
			if(strValue != null && !trim(strValue).equals("")){
				strValue = strValue.replace(",", "");
				iValue = Long.parseLong(strValue);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return iValue;
	}

	
	public static String convertLongToStr(Long iValue) {

		if (iValue == null)
			return "";
		else
			return iValue.toString();

	}

	public static Integer convertStrToInteger(String strValue) {

		Integer iValue = null;
		
		try {
			if(strValue != null && !trim(strValue).equals(""))
				iValue = Integer.parseInt(strValue);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return iValue;
	}

	public static Double convertStrToDouble(String strValue) {

		Double iValue = 0.0;
		if(strValue == null || strValue.equals("")) return null;
		
		try {
			iValue = Double.parseDouble(strValue);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return iValue;
	}

	public static int convertStrToInt(String strValue) {

		Integer iValue = 0;

		try {
			iValue = Integer.parseInt(strValue);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

		return iValue.intValue();
	}

	public static String subString(String strIn, int iFrom, int iLen) {

		String strTmp = "";

		if (strIn != null) {
			if (strIn.length() > (iFrom + iLen)) {
				strTmp = strIn.substring(iFrom, iFrom + iLen);
			} else if (strIn.length() > iFrom) {
				strTmp = strIn.substring(iFrom);
			}
		}

		return strTmp;
	}

	public static String convertCSVStr(Object strValue) {

		String strReturn = "\"\"";

		if (strValue != null) {
			strReturn = "\"" + strValue.toString().trim() + "\"";
		}

		return strReturn;
	}

	public static String convertZipCodeToString(String strZipCode) {
		String result = "";

		if (strZipCode != null) {
			String[] arrStr = strZipCode.split("-");
			for (int i = 0; i < arrStr.length; i++) {
				result += arrStr[i];
			}
		}

		return result;
	}

	public static boolean checkPer(String strValue1, String strValue2) {

		strValue1 = StringUtils.trimAll(strValue1);
		strValue2 = StringUtils.trimAll(strValue2);
		String strTemp = "";

		boolean hasPer = false;
		int iValue = 0;

		while (iValue <= strValue1.length() - 2) {

			strTemp = strValue1.substring(iValue, iValue + 2);

			if (strValue2.equalsIgnoreCase(strTemp)) {
				hasPer = true;
				break;
			}

			iValue = iValue + 2;
		}

		return hasPer;
	}

	public static String nulToString(String str) {
		if (str == null) {
			return "";
		} else {
			return str.trim();
		}
	}

	public static String getJapaneseFromUrl(String utlText) {
		String japanText = "";
		try {
			byte[] byteData;
			byteData = utlText.getBytes("ISO_8859_1");
			japanText = new String(byteData, "UTF8");
			return japanText;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return utlText;
	}

	public static Map<String, String> sortMapByIntKey(Map<String, String> map) {
		Map<String, String> mapResult = new LinkedHashMap<String, String>();
		TreeSet<String> keys = new TreeSet<String>(map.keySet());
		List<Integer> list = new ArrayList<Integer>();
		for (String key : keys) {
			list.add(Integer.parseInt(key));
		}
		Collections.sort(list);
		for (Integer key : list) {
			mapResult.put(StringUtils.convertIntegerToStr(key), map.get(StringUtils.convertIntegerToStr(key)));
		}
		return mapResult;
	}

	public static String trim(String value) {
		return ltrim(rtrim(value));
	}

	public static String ltrim(String value) {

		if (value == null || value.equals("")) {
			return value;
		}

		int pos = 0;

		for (int i = 0; i < value.length(); i++) {
			char c = value.charAt(i);
			if (c != SPACE_CHAR_HALF && c != SPACE_CHAR_WID) {
				break;
			}
			pos = i + 1;
		}

		if (pos > 0) {
			return value.substring(pos);
		} else {
			return value;
		}

	}

	public static String rtrim(String value) {

		if (value == null || value.equals("")) {
			return value;
		}

		int pos = 0;

		for (int i = value.length() - 1; i >= 0; i--) {
			char c = value.charAt(i);
			if (c != SPACE_CHAR_HALF && c != SPACE_CHAR_WID) {
				break;
			}
			pos = i;
		}

		if (pos > 0) {
			return value.substring(0, pos);
		} else {
			return value;
		}

	}

	public static String valueOfCombobox(Object value) {
		String result;
		result = value != null ? trim(value.toString()) : null;
		if (result != null && result.equals("")) {
			result = "â–³";
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public static <T> T convertFromCombobox(String value, Class<T> returnType) {
		if(value == null) return null;
		try { 
			if (trim(value).equals("â–³") || trim(value).length() == 0) {
				if( returnType == Character.class){
					Character result = " ".charAt(0);
					return (T) result;
				}else if(returnType == String.class){
					String result = " ";
					return (T) result;
				}else if(returnType == Integer.class){
					Integer result = 0;
					return (T) result;
				}
			}
			
			if( returnType == Character.class){
				Character result = value.charAt(0);
				return (T) result;
			}else if(returnType == String.class){
				String result = value;
				return (T) result;
			}else if(returnType == Integer.class){
				Integer result = Integer.parseInt(value);
				return (T) result;
			}
		} catch (InputMismatchException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String convertToCombobox(Object value){
		if(value != null){
			if(trim(value.toString()).equals("")){
				try {
					return "â–³";
				} catch (InputMismatchException e) {
					e.printStackTrace();
				}
			}
			return value.toString();
		}
		
		return "";
	}
	
	public static <T> String convertToString(T input){
		if(input == null)
			return "";
		else
			return trim(input.toString());
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T convertFromString(String strValue, Class<T> returnType){
		if(isValid(strValue)){
			strValue = trim(strValue);
			
		  //pattern must be number
			DecimalFormatSymbols dSeparator = new DecimalFormatSymbols();
			String strDecimalPattern = "[^0-9"+ "\\"+ dSeparator.getDecimalSeparator() + "]";
			
			//type: byte, short, integer, long, float, double, boolean, character
			try {
				if(returnType == Byte.class || returnType == byte.class){
					Byte result = Byte.parseByte(strValue);
					return (T) result;
				}else if(returnType == Short.class || returnType == short.class){
					Short result = Short.parseShort(strValue);
					return (T) result;
				}else if(returnType == Integer.class || returnType == int.class){
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Integer result = Integer.parseInt(strValue);
					return (T) result;
				}else if(returnType == Long.class || returnType == long.class){
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Long result = Long.parseLong(strValue);
					return (T) result;
				}else if(returnType == Float.class || returnType == float.class){
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Float result = Float.parseFloat(strValue);
					return (T) result;
				}else if(returnType == Double.class || returnType == double.class){
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Double result = Double.parseDouble(strValue);
					return (T) result;
				}else if(returnType == Boolean.class || returnType == boolean.class){
					Boolean result = Boolean.parseBoolean(strValue);
					return (T) result;
				}else if(returnType == Character.class){
					if(strValue.length() == 1){
						Character result = strValue.charAt(0); 
						return (T) result;
					}
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T convertFromString(String strValue, String returnType){
		if(isValid(strValue)){
			strValue = trim(strValue);
			
			//pattern must be number
			DecimalFormatSymbols dSeparator = new DecimalFormatSymbols();
			String strDecimalPattern = "[^0-9"+ "\\"+ dSeparator.getDecimalSeparator() + "]";
			
			//type: byte, short, integer, long, float, double, boolean, character
			try {
				if(returnType.equalsIgnoreCase("Short")){
					Byte result = Byte.parseByte(strValue);
					return (T) result;
				}else if(returnType.equalsIgnoreCase("Byte")){
					Short result = Short.parseShort(strValue);
					return (T) result;
				}else if(returnType.equalsIgnoreCase("Integer")){
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Integer result = Integer.parseInt(strValue);
					return (T) result;
				}else if(returnType.equalsIgnoreCase("Long")){
					
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Long result = Long.parseLong(strValue);
					return (T) result;
				}else if(returnType.equalsIgnoreCase("Float")){
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Float result = Float.parseFloat(strValue);
					return (T) result;
				}else if(returnType.equalsIgnoreCase("Double")){
					strValue = strValue.replaceAll(strDecimalPattern, "");
					Double result = Double.parseDouble(strValue);
					return (T) result;
				}else if(returnType.equalsIgnoreCase("Boolean")){
					Boolean result = Boolean.parseBoolean(strValue);
					return (T) result;
				}else if(returnType.equalsIgnoreCase("Character")){
					if(strValue.length() == 1){
						Character result = strValue.charAt(0); 
						return (T) result;
					}
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		
		return null;
	}
	
	public static String formatNumber(String value, String pattern){
		String result = "";
		
		if(isValid(value)){
			NumberFormat formatter = new DecimalFormat(pattern);
			result = formatter.format(convertFromString(value, Double.class));
		}
		
		return result;
	}
	
	public static boolean equals(Object strValue1, Object strValue2){
		boolean check = false;
		if(strValue1 == null && strValue2 ==null){
			check = true;
		}else if(strValue1 != null && strValue2 != null){
			if(strValue1 instanceof Character)
				strValue1 = ((Character) strValue1).toString();
			if(strValue2 instanceof Character)
				strValue2 = ((Character) strValue2).toString();
			
			if(strValue1.equals(strValue2)) 
				check = true;
		}
		return check;
	}
	public static void main(String[] args){
		System.out.println(ActionContext.getContext().getLocale());
	}
}
